// pages/girl/girl.js
var utils = require("../../utils/util.js")

/**
 *       {
        "pagePath": "pages/girl/girl",
        "text": "美女",
        "iconPath": "pages/images/girl_normal.png",
        "selectedIconPath": "pages/images/girl_select.png"
      },
 */

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    contentList: [],
    url:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getgirllist()
  },
  
  getgirllist: function () {
    var that = this;
    wx.showLoading({
      title: '正在加载...',
    })

    
    wx.request({
      // 20210524 修改为https://github.com/MZCretin/RollToolsApi#获取福利图片列表
      url: "https://www.mxnzp.com/api/image/girl/list?app_id=nfatjgoxjhlqiksb&app_secret=VFk5OWlFcENEdWlLcWFoMWc3Z0d2QT09&page=" + that.data.page,
      method: "GET",
      success: function (res) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        let code = res.statusCode
        if (code == 200) {
          var contentList = []
          contentList = res.data.data.list
          // var i = 0
          // contentList.forEach(element => {
          //   element.url = element.url.replace("http://gank.io/images", "https://raw.githubusercontent.com/RiberWang/PicGoImage/master/image")
          //   if (element.url.indexOf(".jpg") < 0) {
          //     element.url = element.url + '.jpg'
          //   }
          //   contentList[i] = element
          //   i++
          // });
          if (that.data.page > 1) {
            contentList = that.data.contentList.concat(contentList)
          }

          that.setData({
            contentList: contentList
          })
        }
        else {
          wx.showToast({
            title: res.errMsg,
            icon: 'none'
          })
        }
      },
      fail: function (error) {
        wx.hideLoading()
        wx.stopPullDownRefresh();
        console.log(error)
      }
    })
  },

  previewtap: function(event) {
    var urls = []
    this.data.contentList.forEach(element => {
      urls.push(element.imageUrl)
    });
    this.setData({
      url:urls
    })

    let index = event.currentTarget.dataset.index
    wx.previewImage({
      current:this.data.url[index],
      urls: this.data.url,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      contentList: []
    })
    this.getgirllist()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++
    this.getgirllist()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})